var mydialog;
var addr_id;

function showFamilyAddress(id){
	addr_id=id;
	var oldAddress = $('#'+id).val();
	//alert(oldAddress);
	mydialog=art.dialog({
		title:'地址',
		lock:true,
		ok:function(){
			$("#"+id).val($("#address").val());
		},
		cancel:function(){}
	});
	$.ajax({
		url:'FamilyAddress',
		type:'post',
		Global:false,
		data:{oldAddress:oldAddress,operator:'33'},
		dataType:'html',
		success:function(data){
			mydialog.content(data);
		}
	});
}
//行政區 start
function showAreaInfo(id){
	addr_id = id;
	mydialog=art.dialog({
		title:'行政區',
		width:400,
		lock:true,
		ok:function(){
			$("#"+id).val($("#address").val());
		},
		cancel:function(){}
	});
	$.ajax({
		url:'selectArea',
		type:'post',
		Global:false,
		dataType:'html',
		success:function(data){
			mydialog.content(data);
		}
	});
}
function city_selected(obj){
	var id=$(obj).val();
	var value=$('#city option:selected').text();
	$("#address").val(value);
	if(id!=''){
		$("#country").html("");
		$.ajax({
			url:'selectArea',
			type:'post',
			dataType:'json',
			data:{id:id},
			success:show_area_info
		});
	}
}
function show_area_info(data){
	if(data.status===1){
		$.each(data.data,function(key,value){
			$("#country").append("<option value='"+value["id"]+"' >"+value["name"]+"</option>");
		});
	}
}
function area_selected(obj){
	var value=$("#country option:selected").text();
	$("#address").val(value);
}
//行政區 end

function Del_font(){
	var value=$("#address").val();
	var value_length=value.length;
	value=value.substr(0,value_length-1);
	$("#address").val(value);
}

function checkuniformcode(obj){
	$(obj).attr("title","");
	$(obj).css("border","1px solid #bbb");
	var num=$(obj).val();
	if(num!=""){
		$.ajax({
			type:"post",
			url:"chkUniformCode",
			data:{id:num},
			dataType:"json",
			success:function(data){
				if(data.status!=1){
					$(obj).attr("title","統一編碼輸入錯誤");
					$(obj).css("border","1px solid red");
					alert("統一編碼輸入錯誤");
				}
			}
		});
	}
}

function checkCardNumber(obj){
	$(obj).css("border","1px solid #bbb");
	$(obj).attr("title","");
	var num=$(obj).val();
	if(num!=""){
		$.ajax({
			type:"post",
			url:"chkCardNumber",
			data:{id:num},
			dataType:"json",
			success:function(data){
				if(data.status!=1){
					$(obj).css("border","1px solid red");
					$(obj).attr("title","身份證字號輸入錯誤");
					//error++;
					alert("身份證字號輸入錯誤");
				}
			}
		});
	}
}

function checkUnique(obj,n,t,re){
	var value=$(obj).val();
	var table=t;
	var name=n;
	var remark=re;
	$.ajax({
		type:'post',
		url:'checkUnique_action',
		dataType:'json',
		data:{table:table,name:name,value:value,remark:remark},
		success:unique_status,
		error:function(){alert("error");}
	});
}

function unique_status(data){
	if(data.status===1){
		$("#unique_info").html(data.info);
		$("#unique_info").attr("status","false");
	}else{
		$("#unique_info").html("");
		$("#unique_info").attr("status","true");
	}
}

function city_select(obj){
	var id=$(obj).attr("value");
	var table="address_county";
	var value=$('#city option:selected').text();
	$("#address").val(value);
	if(id!=''){
		$("#country").html("");
		$("#street").html("");
		$.ajax({
			url:'FamilyAddress',
			type:'post',
			dataType:'json',
			data:{id:id,table:table,operator:'33'},
			success:city_address_status
		});
	}
}

function city_address_status(data){
	if(data.status===1){
		$("#country").append("<option value=''>請選擇</option>");
		$.each(data.data,function(key,value){
			$("#country").append("<option value='"+value["id"]+"' >"+value["name"]+"</option>");
		});
	}
}

function country_selected(obj){
	var id=$(obj).attr("value");
	var table="address_street";
	var value=$('#city option:selected').text()+$('#country option:selected').text();
	$("#address").val(value);
	if(id!=''){
		$("#street").html("");
		$.ajax({
			url:'FamilyAddress',
			type:'post',
			dataType:'json',
			data:{id:id,table:table,operator:'33'},
			success:country_address_status
		});
	}
}

function country_address_status(data){
	if(data.status===1){
		$("#street").append("<option value=''>請選擇</option>");
		$.each(data.data,function(key,value){
			$("#street").append("<option value='"+value["id"]+"'>"+value["name"]+"</option>");
		});
	}
}

function street_selected(obj){
	var value=$('#city option:selected').text()+$('#country option:selected').text()+$('#street option:selected').text();
	$("#address").val(value);
}

function link_button_font(obj){
	$("#address").val($("#address").val()+$(obj).val());
}

function show_area_select(){
	mydialog=art.dialog({
		title:'地圖區域',
		lock:true,
		ok:function(){
			$("#ne_lat").val($("#lat1").val());
			$("#ne_lng").val($("#lng1").val());
			$("#sw_lat").val($("#lat2").val());
			$("#sw_lng").val($("#lng2").val());
	},
		cancel:function(){}
	});
	$.ajax({
		url:'map_area',
		type:'post',
		dataType:'html',
		success:function(data){
			mydialog.content(data);
			initialize();
		}
	});
}

//map init
function initialize() {
	var mapOptions = {          
		center: new google.maps.LatLng(24.997344353729364, 121.55073852549200),          
		zoom: 10,          
		mapTypeId: google.maps.MapTypeId.ROADMAP 
	};    
	var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
	map.controls[google.maps.ControlPosition.TOP_CENTER].push(document.getElementById("mycontrol"));
	
	var rectangle=null;
	var click1;
	var mousemove1;
	var rectclick;
	var start=document.getElementById("start");
	var end=document.getElementById("end");
	var area;
	
	google.maps.event.addDomListener(start,"click",function(){
			start.style.fontWeight="bold";
			end.style.fontWeight="normal";
			var pointS=null;
			var pointE=null;
			click1=google.maps.event.addListener(map,"click",function(event1){
				
				mousemove1=google.maps.event.addListener(map,"mousemove",function(event2){
					 pointS=event1.latLng;
					 pointE=event2.latLng;
					 var rectOptions = {
				      strokeColor: "#FF0000",
				      strokeOpacity: 0.8,
				      strokeWeight: 2,
				      fillColor: "#FF0000",
				      fillOpacity: 0.0,
				      draggable:true,
				      editable:true,
				      map: map,
				      bounds: new google.maps.LatLngBounds(pointS,pointE)
				    };
				    if(rectangle!=null){
				    		rectangle.setMap(null);
				    		rectangle=null;
				    }
				    rectangle = new google.maps.Rectangle();
						rectangle.setOptions(rectOptions);
						rectclick=google.maps.event.addListenerOnce(rectangle,"click",function(){
									google.maps.event.removeListener(click1);	
									google.maps.event.removeListener(mousemove1);
									google.maps.event.removeListener(rectclick);
						});
				});
			});
	});
	
	google.maps.event.addDomListener(end,"click",function(){
			start.style.fontWeight="normal";
			end.style.fontWeight="bold";
			rectangle.setMap(null);
			alert("選取成功");
			area=rectangle.getBounds();
			var ne=area.getNorthEast();
			var sw=area.getSouthWest();
			$("#lat1").val(ne.lat());
			$("#lng1").val(ne.lng());
			$("#lat2").val(sw.lat());
			$("#lng2").val(sw.lng());
	});
}

function showBaseInfo(title,name,fields,destination){
	mydialog=art.dialog({
		width:560,
		height:300,
		title:title,
		content:"<iframe src='"+actionurl+"/showBaseInfo?name="+name+"&fields="+fields+"' id='i_frame' height='300' width='560' ></iframe>",
		lock:true,
		ok:function(){
			var ischecked=$(document.getElementById("i_frame").contentWindow.document).find("#show_baseinfo input:radio[name='baseinfo']:checked").val();
			if(ischecked==''||ischecked==undefined){
				alert('請選擇一筆資料');
				return false;
			}else{
				$("#"+destination).val($(document.getElementById("i_frame").contentWindow.document).find("#"+ischecked).html());
			}
		},
		calcel:function(){}
	});
}

function showBaseInfo_status(data){
	if(data!=null&&data!=''&&data!=undefined){
		mydialog.content(data);
	}else{
		mydialog.content('加載失敗');
	}
}

//案名選擇 start
function showAnmingInfo(id){
	mydialog=art.dialog({
		width:500,
		height:340,
		title:'案名',
		content:"<iframe src='"+actionurl+"/showAnmingInfo' id='am_frame' height='340' width='560' ></iframe>",
		lock:true,
		ok:function(){
			var ischecked=$(document.getElementById("am_frame").contentWindow.document).find("#show_aminfo input:radio[name='aminfo']:checked").val();
			if(ischecked==''||ischecked==undefined){
				alert('請選擇一筆資料');
				return false;
			}else{
				$("#"+id).val($(document.getElementById("am_frame").contentWindow.document).find("#am_"+ischecked).html());
			}
		},
		calcel:function(){}
	});
}
//案例選擇 end

//search user tips  start

function lookup(val){
	if(val.length == 0){
		$('#search_user_div').hide();
	}else{
		$.ajax({
			url:'showSearchUser',
			type:'post',
			data:{name:val},
			dataType:'json',
			success:function(data){
				if(data.status === 1){
					$('#search_user_div').show();
					$('#search_user_div').html(data.data);
				}
			},
			error:function(){alert('error');}
		});
	}
}
function fill(v,vo){
	$('#cu_name').val(v);
	$('#cu_recom').val(vo);
	setTimeout("$('#search_user_div').hide();",100);
}

function showUser(type,o_type){
	$('#search_user_div').html('');
	var name=$("#cu_name").val();
	if(name!=''){
		$.ajax({
			url:'showSearchUser',
			type:'post',
			data:{name:name,type:type,o_type:o_type},
			dataType:'json',
			success:showuser_status,
			error:function(){alert('error');}
		});
	}else{
		$('#search_user_div').css("display","none");
	}
}

function showuser_status(data){
	if(data.status===1){
		$('#search_user_div').html(data.data);
		$('#search_user_div').css("display","block");
	}
}

function hidden_userdiv(){
	$('#search_user_div').css("display","none");
}

function setUserName(obj,type){
	if(type==1){
		var gettype=$(obj).attr('gettype');
		if(gettype==1){
			$('#cu_name').val($(obj).text());
			$('#cu_recom').val($(obj).attr('cu_recom'));
		}else if(gettype==2){
			$('#cu_name').val($(obj).attr('cu_name'));
			$('#cu_recom').val($(obj).text());
		}
	}else{
		$('#cu_name').val($(obj).text());
	}
		hidden_userdiv();
		
}
//search user tips  end

function open_email(id){
	var email=$('#'+id).val();
	if(email==''||email==undefined){
		alert('email不能為空');
	}else{
		var regexp=/.*@.*\..*/;
		if(regexp.test(email)){
			location.href='mailto:'+email;
		}else{
			alert('email格式輸入錯誤');
		}
	}
}

//check for input is number
function isNum(obj){
	var value=$(obj).val();
	var regexp=/\D+/;
	if(regexp.test(value)){
		alert('只能輸入數字哦');
		$(obj).val('');
	}
}